module AutoBase
  module Validate
    class ValidateError < StandardError; end
    module ClassMethods;end
    
    module InstanceMethods
      private
      def validate(validatee, validators)
        validators.each_pair do |name, expectation|
          name_to_s = name.to_s
          
          if name_to_s.match(/^not_/)
            validate_true = false
            name_to_s.gsub!(/^not_/, '')
          elsif name_to_s.match(/_not_/)
            validate_true = false
            name_to_s.gsub!(/_not_/, '_')
          else
            validate_true = true
          end

          require "auto_base/validate/#{name_to_s}" # 使用的时候再加载
          validator_name = name_to_s.split("_").map{|e| e.capitalize}.join("")
          validator_class = eval("AutoBase::Validate::" + validator_name)
          validator = validator_class.new(validatee, expectation)
          validator.validate(validate_true)
        end
      end
    end
    
    def self.included(receiver)
      receiver.extend         ClassMethods
      receiver.send :include, InstanceMethods
    end
    
    extend InstanceMethods
  end
end